<template>
  <div class="admin-login">
    <el-card class="login-card">
      <h1>管理员登录</h1>
      <el-form :model="loginForm" :rules="rules" ref="loginForm" @submit.native.prevent="submitForm">
        <el-form-item prop="email">
          <el-input v-model="loginForm.email" placeholder="管理员邮箱" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="密码" prefix-icon="el-icon-lock" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" native-type="submit" :loading="loading">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { setToken } from '@/utils/auth'

export default {
  name: 'AdminLogin',
  data() {
    return {
      loading: false,
      loginForm: {
        email: '',
        password: ''
      },
      rules: {
        email: [
          { required: true, message: '请输入管理员邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱格式', trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
        ]
      },
      // 硬编码的管理员凭证
      adminCredentials: [
        {
          email: 'admin@oldphonedeals.com',
          password: 'admin123',
          name: '超级管理员'
        },
        {
          email: 'manager@oldphonedeals.com',
          password: 'manager123',
          name: '普通管理员'
        }
      ]
    }
  },
  methods: {
    submitForm() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          
          // 验证管理员凭证
          const admin = this.adminCredentials.find(
            cred => cred.email === this.loginForm.email && 
                   cred.password === this.loginForm.password
          )
          
          if (admin) {
            // 模拟API调用延迟
            setTimeout(() => {
              setToken('admin-token')
              localStorage.setItem('adminName', admin.name)
              this.$router.push('/admin/dashboard')
              this.loading = false
            }, 500)
          } else {
            this.loading = false
            this.$message.error('管理员邮箱或密码错误')
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.admin-login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
}

.login-card {
  width: 400px;
  padding: 30px;
}

h1 {
  text-align: center;
  margin-bottom: 30px;
  color: #303133;
}

.el-button {
  width: 100%;
}
</style>